set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-format -DTEE_UNTRUSTED")

find_package(Protobuf REQUIRED)
file(GLOB PROTO_FILES
    ${UAL_TOP_DIR}/proto/attestation.proto
    ${UAL_TOP_DIR}/proto/crypto.proto
)
PROTOBUF_GENERATE_CPP(PROTO_SRCS PROTO_HDRS ${CMAKE_BINARY_DIR} ${PROTO_FILES})

file(GLOB COMMON_SRCS ${UAL_TOP_DIR}/common/*.cpp)
file(GLOB COMMON_PLATFORMS_SRCS ${UAL_TOP_DIR}/common/platforms/*.cpp)
file(GLOB UTILS_SRCS ${UAL_TOP_DIR}/utils/untrusted/*.cpp)
file(GLOB CORE_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/../core/*.cpp)
file(GLOB UNTRUSTED_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp)
# Add the platform files
file(GLOB PLATFORM_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/../platforms/*.cpp)
# Add the platform special files for UAS
file(GLOB UAS_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/../uas/*.cpp)
# Add the platform special files for SGX1 EPID
file(GLOB PLATFORM_SGX1_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/../platforms/sgx1/*.cpp)
# Add the platform special files for HyperEnclave
file(GLOB PLATFORM_HYEN_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/../platforms/hyperenclave/*.cpp)
# Add the platform special files for SGX2 DCAP
file(GLOB_RECURSE PLATFORM_SGX2_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/../platforms/sgx2/*.cpp)
set(PLATFORM_SRC_DIR_SGX2_QVL ${CMAKE_CURRENT_SOURCE_DIR}/../platforms/sgx2/qvl)
# Add the platform special files for CSV
file(GLOB_RECURSE PLATFORM_CSV_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/../platforms/csv/*.cpp)
# Add the platform special files for Kunpeng
file(GLOB_RECURSE PLATFORM_KUNPENG_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/../platforms/kunpeng/*.cpp)
set(PLATFORM_INCS
    ./
    ${PLATFORM_SRC_DIR_SGX2_QVL}
    ${PLATFORM_SRC_DIR_SGX2_QVL}/include
    ${PLATFORM_SRC_DIR_SGX2_QVL}/AttestationCommons/include
    ${PLATFORM_SRC_DIR_SGX2_QVL}/AttestationCommons/include/Utils
    ${PLATFORM_SRC_DIR_SGX2_QVL}/AttestationLibrary/include
    ${PLATFORM_SRC_DIR_SGX2_QVL}/AttestationLibrary/src
    ${PLATFORM_SRC_DIR_SGX2_QVL}/AttestationParsers/include
    ${PLATFORM_SRC_DIR_SGX2_QVL}/AttestationParsers/src
)

# Build the ua_uverification libray
set(LIB_UA_UVERIFICATION ua_uverification)
set(U_SRCS
    ${PROTO_SRCS}
    ${COMMON_SRCS}
    ${COMMON_PLATFORMS_SRCS}
    ${UTILS_SRCS}
    ${CORE_SRCS}
    ${PLATFORM_SRCS}
    ${UAS_SRCS}
    ${PLATFORM_SGX1_SRCS}
    ${PLATFORM_SGX2_SRCS}
    ${PLATFORM_HYEN_SRCS}
    ${PLATFORM_CSV_SRCS}
    ${PLATFORM_KUNPENG_SRCS}
    ${UNTRUSTED_SRCS}
)

add_library(${LIB_UA_UVERIFICATION} ${UA_LIB_TYPE} ${U_SRCS})

target_include_directories(
    ${LIB_UA_UVERIFICATION} PUBLIC
    ${PLATFORM_INCS}
    ${CMAKE_BINARY_DIR}
    ${CMAKE_CURRENT_BINARY_DIR}
    ${UAL_TOP_DIR}
    ${UAL_TOP_DIR}/include
    ${UAL_TOP_DIR}/include/sgx
    ${UAL_TOP_DIR}/external/cppcodec
    ${UAL_TOP_DIR}/external/rapidjson/include
    ${SGX_INCLUDE_DIR}
    ${OPENSSL_INC_DIR}
)
target_link_libraries(
    ${LIB_UA_UVERIFICATION}
    -L${LIBRARY_OUTPUT_PATH}
    -lcrypto -lprotobuf
)
add_dependencies(${LIB_UA_UVERIFICATION} ua_unetwork)

install(
    TARGETS ${LIB_UA_UVERIFICATION}
    DESTINATION ${CMAKE_INSTALL_PREFIX}/lib
)
